package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.BaiKFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.SheQFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.ShouYFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.WoDFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener{

    LinearLayout tab_sy;
    LinearLayout tab_sq;
    LinearLayout tab_bk;
    LinearLayout tab_wd;
    LinearLayout tab_sc;

    ImageView img_sy;
    ImageView img_sq;
    ImageView img_bk;
    ImageView img_wd;

    TextView tv_sy;
    TextView tv_sq;
    TextView tv_bk;
    TextView tv_wd;

    Fragment fragment_baike;
    Fragment fragment_shequ;
    Fragment fragment_shouye;
    Fragment fragment_wode;

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_main);

        tab_bk = (LinearLayout)findViewById(R.id.tab_bk);
        tab_sq = (LinearLayout)findViewById(R.id.tab_sq);
        tab_wd = (LinearLayout)findViewById(R.id.tab_wd);
        tab_sy = (LinearLayout)findViewById(R.id.tab_sy);
        tab_sc = (LinearLayout)findViewById(R.id.tab_sc);

        img_bk = (ImageView)findViewById(R.id.img_bk);
        img_sq = (ImageView)findViewById(R.id.img_sq);
        img_wd = (ImageView)findViewById(R.id.img_wd);
        img_sy = (ImageView)findViewById(R.id.img_sy);

        tv_bk = (TextView)findViewById(R.id.tv_bk);
        tv_sq = (TextView)findViewById(R.id.tv_sq);
        tv_wd = (TextView)findViewById(R.id.tv_wd);
        tv_sy = (TextView)findViewById(R.id.tv_sy);
        selectTab(0);
    }

    @Override
    void initEvent() {
        tab_sy.setOnClickListener(this);
        tab_sq.setOnClickListener(this);
        tab_bk.setOnClickListener(this);
        tab_wd.setOnClickListener(this);
        tab_sc.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }

    private void selectTab(int i) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i) {
            case 0:
                if (fragment_shouye == null) {
                    fragment_shouye = new ShouYFragment();
                    fragmentTransaction.add(R.id.content, fragment_shouye);
                } else {
                    fragmentTransaction.show(fragment_shouye);
                }
                //tv_px.setTextColor(0x2c2c2cdd);
                img_sy.setImageResource(R.mipmap.syl);

                break;
            case 1:
                if (fragment_shequ == null) {
                    fragment_shequ = new SheQFragment();
                    fragmentTransaction.add(R.id.content, fragment_shequ);
                } else {

                    fragmentTransaction.show(fragment_shequ);
                }
                //tv_jlb.setTextColor(0x2c2c2cdd);
                img_sq.setImageResource(R.mipmap.sql);
                break;
            case 2:
                if (fragment_baike == null) {
                    fragment_baike = new BaiKFragment();
                    fragmentTransaction.add(R.id.content, fragment_baike);
                } else {
                    fragmentTransaction.show(fragment_baike);
                }
                img_bk.setImageResource(R.mipmap.kpl);
                //tv_lm.setTextColor(0x2c2c2cdd);
                break;

            case 3:
                if (fragment_wode == null) {
                    fragment_wode = new WoDFragment();
                    fragmentTransaction.add(R.id.content, fragment_wode);
                } else {
                    fragmentTransaction.show(fragment_wode);
                }
                //tv_st.setTextColor(0x2c2c2cdd);
                img_wd.setImageResource(R.mipmap.wdl);
                break;

            default:
                break;
        }
        fragmentTransaction.commit();
    }

    private void resetImage() {
        img_bk.setImageResource(R.mipmap.kph);
        img_sq.setImageResource(R.mipmap.sqh);
        img_sy.setImageResource(R.mipmap.syh);
        img_wd.setImageResource(R.mipmap.wdh);
    }
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (fragment_shouye != null) {
            fragmentTransaction.hide(fragment_shouye);
        }
        if (fragment_shequ != null) {
            fragmentTransaction.hide(fragment_shequ);
        }
        if (fragment_baike != null) {
            fragmentTransaction.hide(fragment_baike);
        }
        if (fragment_wode != null) {
            fragmentTransaction.hide(fragment_wode);
        }
    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {
            case R.id.tab_sy:
                resetImage();
                selectTab(0);
                break;
            case R.id.tab_sq:
                resetImage();
                selectTab(1);
                break;
            case R.id.tab_bk:
                resetImage();
                selectTab(2);
                break;
            case R.id.tab_wd:
                resetImage();
                selectTab(3);
                break;
            case R.id.tab_sc:
                Intent intent = new Intent(MainActivity.this,ShopMainActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }

    }
}
